#include "CSettingWnd.h"

#include <QDir>
#include <QMessageBox>

#include "Version.h"

CSettingWnd::CSettingWnd(QWidget* parent) :
    QWidget(parent)
{
    ui.setupUi(this);
    setWindowTitle(tr("zhilaiSDK tool") + " " + PROJECT_VERSION);

    ui.combo_mcb_uuid->addItem("顺丰监控板通讯协议_V3.3", "F705E0ED69E363DCA6A174DE64326267");
    ui.combo_mcb_uuid->addItem("标准快件箱监控板_V1.0.0.200901", "F705E0ED69E363DCA6A174DE64326267");
}

CSettingWnd::~CSettingWnd()
{
}

void CSettingWnd::showEvent(QShowEvent* e)
{
    QWidget::showEvent(e);

    on_btn_reload_clicked();
}

void CSettingWnd::on_btn_add2_clicked()
{
    auto number = ui.edt_dcb_number->text().toInt();
    auto addr = ui.edt_dcb_addr->text().toStdString();
    auto count = ui.edt_dcb_count->text().toInt();

    dcb_list_.push_back(std::make_tuple(number, count, addr));
    ui.edt_board->append(QString("%1\t%2\t%3").arg(number).arg(count).arg(addr.c_str()));
}

void CSettingWnd::on_btn_clear2_clicked()
{
    ui.edt_board->clear();
    config_.GetDevice().clear();
    dcb_list_.clear();

    QString title = tr("Number") + "\t" + tr("Count") + "\t" + tr("Addr");
    ui.edt_board->append(title);
}

void CSettingWnd::on_btn_save_clicked()
{
    auto addr = ui.edt_mcb_addr->text().toStdString();
    auto mcb_uuid = ui.combo_mcb_uuid->currentData().toString().toStdString();
    config_.GetDevice().clear();

    // 添加监控板
    if (!addr.empty())
    {
        CZlBaseConfig::ZLBaseDevice mcb;
        mcb.number = 0;
        mcb.count = 0;
        mcb.type = zl::zlbase::EDeviceType_MCB;
        mcb.uuid = mcb_uuid.empty() ? "4936DB3603F4FD7EE39BAE7E03905E40" : mcb_uuid;
        mcb.datalink.type = zl::zlbase::EDatalinkType_EthernetUdp;
        mcb.datalink.is_shared = true;
        mcb.datalink.localport = 30000;
        mcb.datalink.remoteport = 30000;
        strcpy(mcb.datalink.remoteaddr, addr.c_str());
        config_.GetDevice().push_back(mcb);
    }

    // 添加开门板
    for (const auto i : dcb_list_)
    {
        CZlBaseConfig::ZLBaseDevice device;
        device.number = std::get<0>(i);
        device.count = std::get<1>(i);
        device.type = zl::zlbase::EDeviceType_DCB;
        device.uuid = "BC7DC1534139E574389F41EDDA0400A2";  // 丰巢开门板通讯协议_V6.06
        device.datalink.type = zl::zlbase::EDatalinkType_EthernetUdp;
        device.datalink.is_shared = true;
        device.datalink.localport = 30000;
        device.datalink.remoteport = 30000;
        strcpy(device.datalink.remoteaddr, std::get<2>(i).c_str());
        config_.GetDevice().push_back(device);
    }

    // 创建目录
    QDir dir("c:\\zhilai");
    if (!dir.exists())
    {
        if (!dir.mkpath("c:\\zhilai"))
        {
            QMessageBox::warning(this, tr("Error"), tr("Create config file directory failed"));
            return;
        }
    }

    if (!config_.Save())
    {
        QMessageBox::warning(this, tr("Error"), tr("save config file failed"));
    }
    else
    {
        QMessageBox::information(this, tr("success"), tr("save config file success"));
    }
}

void CSettingWnd::on_btn_reload_clicked()
{
    on_btn_clear2_clicked();

    if (!config_.Load())
    {
        QMessageBox::warning(this, tr("Error"), tr("load config file failed"));
    }

    for (const auto& device : config_.GetDevice())
    {
        if (device.type == zl::zlbase::EDeviceType_MCB)
        {
            auto index = ui.combo_mcb_uuid->findData(device.uuid.c_str());
            ui.combo_mcb_uuid->setCurrentIndex(index);
            ui.edt_mcb_addr->setText(device.datalink.remoteaddr);
            continue;
        }

        ui.edt_board->append(QString("%1\t%2\t%3").arg(device.number).arg(device.count).arg(device.datalink.remoteaddr));
        dcb_list_.push_back(std::make_tuple(device.number, device.count, std::string(device.datalink.remoteaddr)));
    }
}